﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PRET.ReportControlLib.Controls
{
    public partial class SetYearPanel : UserControl
    {
        public event ShowYearReportViewEventHandle showReportEvent;
        private Dictionary<string, int> efficiencyDic = new Dictionary<string, int>();

        private readonly static int StartYear = 2014;
        public SetYearPanel()
        {
            InitializeComponent();
            InitializeYears();
        }

        public void EnableControls(bool sign)
        {
            this.yearCombox.Enabled = sign;
            this.typeComboBox.Enabled = sign;
            this.quotaTextBox.Enabled = sign;
            this.reportViewBtn.Enabled = sign;
        }

        public DateTime GetSelectedYear
        {
            get
            {
                string year = string.Format("{0}-01-01 00:00:00", this.yearCombox.SelectedItem.ToString());
                return DateTime.Parse(year);
            }
        }

        public int GetQuota
        {
            get
            {
                int quota = 0;
                int.TryParse(this.quotaTextBox.Text, out quota);
                return quota;
            }
        }
        public int SetQuota
        {
            set
            {
                this.quotaTextBox.Text = Convert.ToString(value);
            }
        }

        private bool IsCorrectYear()
        {
            if (this.yearCombox.SelectedItem == null)
                return false;
            return true;
        }

        private void InitializeYears()
        {
            List<string> ds = new List<string>();
            for (int i = DateTime.Now.Year; i >= StartYear; i--)
            {
                ds.Add(i.ToString());
            }
            this.yearCombox.DataSource = ds;
        }

        public void SetEfficiencyList(Dictionary<string, int> effDic)
        {
            if (effDic != null && effDic.Count > 0)
            {
                this.efficiencyDic.Clear();
                List<string> typeList = new List<string>();
                foreach(KeyValuePair<string, int> item in effDic)
                {
                    this.efficiencyDic.Add(item.Key, item.Value);
                    typeList.Add(item.Key);
                }
                //typeList.Sort((left, right) =>
                //{
                //    if (left[0] == right[0])
                //    {
                //        int a = int.Parse(left.Substring(1));
                //        int b = int.Parse(right.Substring(1));
                //        return a - b;
                //    }
                //    else
                //    {
                //        return string.Compare(left, right);
                //    }
                //});
                this.typeComboBox.DataSource = typeList;
            }
        }

        private void reportViewBtn_Click(object sender, EventArgs e)
        {
            if (showReportEvent != null)
            {
                if (!this.IsCorrectYear())
                {
                    MessageBox.Show("请从列表中选择正确的年份！", "操作错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                int quota = 0;
                if (!int.TryParse(this.quotaTextBox.Text, out quota))
                {
                    MessageBox.Show("请输入正确的效率指标！", "操作错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                
                ShowYearReportViewEventArgs eventArg = new ShowYearReportViewEventArgs();
                eventArg.StartYear = this.GetSelectedYear;
                eventArg.Quota = this.efficiencyDic;
                showReportEvent(this, eventArg);
            }
        }

        private void typeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.quotaTextBox.Text = this.efficiencyDic[this.typeComboBox.SelectedItem.ToString()].ToString();
        }

        private void quotaTextBox_TextChanged(object sender, EventArgs e)
        {
            int quota = 0;
            if (int.TryParse(this.quotaTextBox.Text, out quota))
                this.efficiencyDic[this.typeComboBox.SelectedItem.ToString()] = quota;
            else
                MessageBox.Show("请输入正确的效率指标！", "操作错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }



    public delegate void ShowYearReportViewEventHandle(object sender, ShowYearReportViewEventArgs args);

    public class ShowYearReportViewEventArgs : EventArgs
    {
        public ShowYearReportViewEventArgs()
        { }
        private DateTime startYear;
        public DateTime StartYear
        {
            get { return startYear; }
            set { startYear = value; }
        }

        private Dictionary<string, int> quota;
        public Dictionary<string, int> Quota
        {
            get { return quota; }
            set { quota = value; }
        }
    }
}
